#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : xuexiaohua
# @time    : 2025/8/20 13:38
# @function: the script is used to do something.
# @version :

import pymysql

host = 'mysql.hogwarts.ceshiren.com'
port = 3306
user = 'stu'
password = 'hogwarts_stu'
db = 'hogwarts_stu'

class Database:
    def __init__(self):
        self.conn = pymysql.connect(
            host = host,
            port = port,
            user = user,
            password = password,
            database = db
        )

    def get_connection(self):
        return self.conn

    def close_connection(self):
        self.conn.close()

# 装饰器数据库链接
def mysql_con(func):
    def sql_execute(sql, *args):
        con = Database().get_connection()
        cursor = con.cursor()
        result = func(cursor, sql, *args)
        con.commit()
        cursor.close()
        Database().close_connection()
        return result
    return sql_execute

@mysql_con
def execute_sql(cursor,sql, *args):
    cursor.execute(sql, *args)
    result = cursor.fetchall()
    return result
